<?php

namespace app\admin\model\test\dyzn;

use app\admin\model\User;
use think\Db;
use think\Model;

class Answer extends Model
{
    // 表名
    protected $name = 'test_dyzn_answer';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    
    // 追加属性
    protected $append = [
        'create_time_text',
        'update_time_text',
        'user_nickname',
        'user_mobile'
    ];
    

    



    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }


    public function getUserNicknameAttr($value,$data){
        return User::where('id',$data['user_id']) -> value('nickname');
    }

    public function getUserMobileAttr($value,$data){
        $user = (new User()) -> where(['id' => $data['user_id']]) -> find();
        return $user -> mobile;
//        return User::get($data['user_id']) -> value('mobile');
    }
}
